#include <iostream>
#include <signal.h>
#include <unistd.h>
#include "algoHandler/AlgoHandler.h"
#include "config/Config.h"
#include "WebSocketModule/WebSocketHandler.h"

bool gExit = false;

void signature(int code) {
    switch(code) {
        case SIGINT: {
            fprintf(stderr, "SIGINT\n");
            gExit = true;
            break;
        }
        default:
            break;
    }
}

void test_recv(uint8_t* data, uint32_t length) {
    data[length + 1] = '\0';
    fprintf(stderr, "%s\r\n", data);
}

int main(int argc, char* argv[]) {
    signal(SIGINT, signature);
    Config::getInstance()->init(DEFAULT_CONFIGPATH);
    AlgoHandler* handler = AlgoHandler::getInstance();
    int rc = 0;
    int try_time = 0;
    do {
        rc = handler->init();
        if(rc != 0) {
            try_time++;
            sleep(1);
        }
    }while(rc != 0 && try_time < 2);

    if(0 != rc) {
        return rc;
    }

    try_time = 0;
    do {
        rc = initWebSocketHandler(Config::getInstance()->hostAddress);
        if(rc != 0) {
            try_time++;
            sleep(3);
        }
    }while(rc != 0 && try_time < 4);

    // if(0 != rc) {
    //     return rc;
    // }

    while(!gExit) {
        sleep(1);
    }

    deInitWebSocketHandler();
    AlgoHandler::getInstance()->releaseMvMedia();
    return 0;
}